package com.beilang.hotline.issue.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.issue.model.IssueModel;
import com.beilang.hotline.issue.to.SearchTO;

@SuppressWarnings("unchecked")
public class IssueDAO extends PaginationBaseDAO {
	public IssueModel createIssue(IssueModel issueModel) {
        issueModel.setDeleteInd("N");
        return (IssueModel)this.save(issueModel);
	}
	
	public IssueModel saveIssue(IssueModel issueModel) {
		return (IssueModel)this.save(issueModel);
	}
	
	public void deleteIssue(Long issueId){
        IssueModel issue= this.getIssue(issueId);
        issue.setDeleteInd("Y");
        this.save(issue);
	}

	public IssueModel getIssue(Long issueId){
		return (IssueModel)this.get(IssueModel.class, issueId);
	}
	
	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap(); 
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String issueName=StringUtil.convertEmpty2Null(searchTO.getIssueName());
        if(issueName!=null)issueName="%"+issueName+"%";
        map.put("issueName", issueName);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchIssueCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String issueName=StringUtil.convertEmpty2Null(searchTO.getIssueName());
        if(issueName!=null)issueName="%"+issueName+"%";
        map.put("issueName", issueName);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchIssueList", map);
    }
    
    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("sysName")) return "system_name";
    	if(dataField.equalsIgnoreCase("issueName")) return "issue_name";
    	return dataField;
    }
    
    public void deleteAllSysIssues(Long sysId){
        Map map = new HashMap();
        map.put("sysId", sysId);
        getSqlMapClientTemplate().update("deleteAllSysIssues",map);
    }

}
